package com.e1xp.deport_management_system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.e1xp.deport_management_system.entity.Io;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author E1xP
 * @since 2021-04-09
 */
public interface IoMapper extends BaseMapper<Io> {

    /**
     * 获取最近count条IO记录
     * @param count 记录条数
     * @return Map
     */
    @Select("SELECT io.id,io.time,io.count," +
            "v.id as vendors_id,v.name as vendors_name," +
            "p.id as product_id,p.name as product_name," +
            "r.id as rack_id,r.name as rack_name FROM io " +
            "left join vendors v on io.vendors_id = v.id " +
            "left join product p on io.product_id = p.id " +
            "left join rack r on io.rack_id = r.id " +
            "WHERE io.type=0 and io.instance_id=#{instanceId} " +
            "Order BY io.time DESC LIMIT #{count}")
    List<Map<String, String>> getRecentIO(@Param("instanceId") long instanceId, @Param("count") int count);

    @Insert("<script>" +
            "INSERT INTO io " +
            "(time,type,rack_id,product_id,count,operator,instance_id) VALUES " +
            "<foreach item='item' collection='ioList' separator=','>" +
            "(#{item.time},#{item.type},#{item.rackId},#{item.productId},#{item.count},#{item.operator},#{item.instanceId})" +
            "</foreach>" +
            "</script>")
    void insertList(@Param("ioList") List<Io> ioList);
}
